<?php   

/**********************************************************
  @Author: Mohannad El-Barachi
  @Copyright: ICORPA 2011
  Index Controller
  *********************************************************/
    
class IndexController extends Zend_Controller_Action   
{   
	    function init()
	    {}
	    
	    function preDispatch() 
	    {}

	    public function indexAction()
	    {
	    	// Start the Pledges object
	    	$Pledge = new Pledges();
	    	
	    	// Get the latest 10 pledges
	    	$this->view->pledges = $Pledge->get_latest_pledges();
	    	
	    	// Get the total number of pledges
	    	$this->view->pledges_counter = $Pledge->get_pledge_counter(1);
	    	
	    	// Get the distinct number of countries
	    	$this->view->pledge_countries = $Pledge->get_distinct_countries();
	    	
	    	// Get the distinct number of areas of exptertise
	    	$this->view->pledge_fields = $Pledge->get_distinct_areas_of_expertise();
	    }
	    
	    
	    public function pledgeAction()
	    {
	    	// Star the Security Object
	    	$Security = new Security();
	    	
	    	// Perform the security check
	    	$Security_Check = $Security->allowed_to_execute_ajax('index');	    	
	    	if (!$Security_Check['result'])
	    	{
	    		print $Security_Check['message'];
	    		exit;
	    	}
	    	
		    // Check if the form has been posted
		    if ($this->_request->isPost()) 
		    {
				// Collect the data from the form & strip all tags
			    Zend_Loader::loadClass('Zend_Filter_StripTags');
			    $f = new Zend_Filter_StripTags();
			    $name = $f->filter(stripslashes($this->_request->getPost('name')));
			    $email = $f->filter($this->_request->getPost('email'));
			    $phone = $f->filter($this->_request->getPost('phone'));
			    $country = $f->filter($this->_request->getPost('country'));
			    $anonymous = $f->filter($this->_request->getPost('anonymous'));
			    $field = $f->filter($this->_request->getPost('field'));
			    $field_other = $f->filter($this->_request->getPost('field_other'));
			    $pledge = $f->filter(stripslashes($this->_request->getPost('user_pledge')));
			    $willingness = $this->_request->getPost('willingness');
			    $city = $this->_request->getPost('city');
			    
			    if ($field == 'Other' && !empty($field_other))
			    {
			    	$field = $field_other;
			    }			    
			    
			    // Create the pledge
			    $Pledge = new Pledges();
			    $new_pledge_id = $Pledge->create_pledge($name,$email,$phone,$country,$anonymous,$pledge,$field,$willingness,$city);
			    if ($new_pledge_id)
			    {
			    	// Email the verification link
			    	$email_subject = "Thank you for your pledge to Egypt";
			    	$email_content = "Dear " . $name . "<br><br>";
			    	$email_content .= "Your pledge to Egypt has been recorded. ";
			    	$email_content .= "Please confirm your pledge by clicking on the following URL <a href='http://www.ipledgetoegypt.org/validate/" . $new_pledge_id*293028390 . "'>http://www.ipledgetoegypt.org/validate/" . $new_pledge_id*293028390 . "</a><br><br>";
					$email_content .= "We thank you for taking the time to register your pledge, and look forward to building a safer, stronger, united Egypt with you.<br><br>";
					$email_content .= "Sincerly,<br>The I Pledge To Egypt Team";

					$mail = new Zend_Mail();
					$mail->setBodyHtml($email_content);
					$mail->setFrom('support@ipledgetoegypt.org', 'I Pledge To Egypt');
					$mail->addTo($email, $name);
					$mail->setSubject($email_subject);
					$mail->send();
			    	
			    	// Tweet it
	    			$tweet = "New pledge: " . substr($pledge,0,120);

	    			// Prepare oAuth options
			    	$oauthOptions = array(
					    'version'         => 1.0,
					    'signatureMethod' => 'HMAC-SHA1',
					    'requestScheme'   => 'header',
					    'consumerKey'     => 'nbb4GwNpli7pjWaByeQkFg',
					    'consumerSecret'  => '8uTGJqnALN5F1f5ONx6kFcP7UvLdiYjvfHHpWYJvg'
					);
					
					// Generate a personal access token, only valid for the application you got the keys from.
					$accessToken = new Zend_Oauth_Token_Access();
					$accessToken->setToken('245571325-bufaOj9Gza4b9EvPWOas5UZOpTtKl8BXgr6rrKDl')
					            ->setTokenSecret('BSZSGpI1bXDfvGR8WbWRtNItytZkFoPOjJeLbl8KM0');
					
					// Set up the service and pass in the http client from the access token.
					$twitter = new Zend_Service_Twitter();
					$twitter->setLocalHttpClient($accessToken->getHttpClient($oauthOptions));
					
					// Make the tweet
					$response = $twitter->status->update($tweet);
					if($response)
					{
						print '<div class="status ui-state-highlight ui-corner-all">Thank you for your pledge! You will receive an email shortly to verify your information.</div>';
					}
					else
					{
						print '<div class="status ui-state-error ui-corner-all">Sorry, there was an error. Please email us at support@ipledgtoegypt.org.</div>';
					}			    	
			    }
			    else
			    {
			    	print '<div class="status ui-state-error ui-corner-all">Sorry, there was an error. Please email us at support@ipledgtoegypt.org.</div>';
			    }
			    exit;
		    }
	    }
}

?>
